﻿#include "canvasitem.h"
#include <QGraphicsSceneMouseEvent>
#include <QStyleOptionGraphicsItem>
#include <QPainter>
#include <QDebug>


CanvasItem::CanvasItem(QGraphicsItem *parent)
{
    // 可选择、可移动
    setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable);
}

void CanvasItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        if (event->modifiers() == Qt::ShiftModifier) {
            qDebug() << "Custom item left clicked with shift key.";
            // 选中 item
            setSelected(true);
        } else if (event->modifiers() == Qt::AltModifier) {
            qDebug() << "Custom item left clicked with alt key.";
            // 重置 item 大小
            double radius = boundingRect().width() / 2.0;
            QPointF topLeft = boundingRect().topLeft();
            m_centerPointF = QPointF(topLeft.x() + pos().x() + radius, topLeft.y() + pos().y() + radius);
            QPointF pos = event->scenePos();
            qDebug() << boundingRect() << radius << this->pos() << pos << event->pos();
            double dist = sqrt(pow(m_centerPointF.x()-pos.x(), 2) + pow(m_centerPointF.y()-pos.y(), 2));
            if (dist / radius > 0.8) {
                qDebug() << dist << radius << dist / radius;
                m_mode = ItemMode_Resize;
            } else {
                m_mode = ItemMode_Select;
            }
        } else {
            qDebug() << "Custom item left clicked.";
            QGraphicsItem::mousePressEvent(event);
            event->accept();
        }
    } else if (event->button() == Qt::RightButton) {
        qDebug() << "Custom item right clicked.";
        event->ignore();
    }
}

void CanvasItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if (m_mode == ItemMode_Resize) {
        //qDebug() << "resizing";
        QPointF pos = event->scenePos();
        double dist = sqrt(pow(m_centerPointF.x()-pos.x(), 2) + pow(m_centerPointF.y()-pos.y(), 2));
        //setRect(m_centerPointF.x()-this->pos().x()-dist, m_centerPointF.y()-this->pos().y()-dist, dist*2, dist*2);
    } else if(event->modifiers() != Qt::AltModifier) {
        //qDebug() << "Custom item moved.";
        QGraphicsItem::mouseMoveEvent(event);
        //qDebug() << "moved" << pos();
    }
}

void CanvasItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    if (m_mode == ItemMode_Resize) {
        m_mode = ItemMode_Select;
    } else {
        QGraphicsItem::mouseReleaseEvent(event);
    }
}

void CanvasItem::setItemMode(ItemMode mode)
{
    m_mode = mode;
}

ItemMode CanvasItem::getItemMode()
{
    return m_mode;
}

QRectF CanvasItem::boundingRect() const
{
    return shape().boundingRect();
}

void CanvasItem::paint(QPainter *painter,
                       const QStyleOptionGraphicsItem *option,
                       QWidget *widget)
{
    Q_UNUSED(widget);
    if (option->state & QStyle::State_Selected) {
        painter->setRenderHint(QPainter::Antialiasing, true);
        if (option->state & QStyle::State_HasFocus) {
            painter->setPen(QPen(Qt::yellow, 3));
        }
        else {
            painter->setPen(Qt::white);
        }
        painter->drawRect(boundingRect());
    }
    painter->setRenderHint(QPainter::Antialiasing, false);
}
